﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYElectricSetting;

namespace HYRElectric.WirePlace
{
	public partial class DlgWireParam : System.Windows.Forms.Form
	{
		public DlgWireParam(Document m_doc, Wire wire)
		{
			this.baseWire = wire;
			this.InitializeComponent();
			this.nudWireNum.TextChanged += this.nudWireNum_TextChanged;
			this.doc = m_doc;
		}

		private void WireParamNon_modeForm_Load(object sender, EventArgs e)
		{
			this.Failed = false;
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeWireConduitInfo();
			try
			{
				this.xmlHelper.LoadXml(this.path, "Root");
			}
			catch (Exception)
			{
				File.Delete(this.path);
			}
			try
			{
				this.FillFormData();
				this.LoadDlgData();
				if (this.baseWire != null)
				{
					Parameter parameter = this.baseWire.GetParameter("相对标高");
					this.txtLevel.Text = parameter.AsDouble().ToString();
					foreach (object obj in this.cmbWireType.Items)
					{
						RevitWireType revitWireType = (RevitWireType)obj;
						if (revitWireType.Name == this.baseWire.Name)
						{
							this.cmbWireType.SelectedItem = revitWireType;
							break;
						}
					}
					for (int i = 0; i < this.cmbConduitType.Items.Count; i++)
					{
						if (this.cmbConduitType.Items[i].ToString() == this.baseWire.GetParameter("线管类型").AsString())
						{
							this.cmbConduitType.SelectedIndex = i;
							break;
						}
					}
					string b = this.baseWire.GetParameter("敷设方式").AsString();
					for (int j = 0; j < this.cmbLayMode.Items.Count; j++)
					{
						if (this.cmbLayMode.Items[j].ToString() == b)
						{
							this.cmbLayMode.SelectedIndex = j;
							break;
						}
					}
					this.cmbCrossSection.SelectedText = this.baseWire.GetParameter("截面").AsString();
					this.nudWireNum.Value = this.baseWire.HotConductorNum;
				}
			}
			catch
			{
			}
		}

		private void InitializeWireConduitInfo()
		{
			this.revitWireTypes = ElectricSettingDataAssist.GetRevitWireTypes(this.doc);
			this.revitConduitTypes = ElectricSettingDataAssist.GetRevitConduitTypes(this.doc);
			if (this.revitWireTypes.Count == 0)
			{
				throw new Exception("请先进行导线设置。");
			}
			if (this.revitConduitTypes.Count == 0)
			{
				throw new Exception("请先进行线管设置。");
			}
		}

		private void FillFormData()
		{
			try
			{
				this.nudWireNum.Value = 1m;
				foreach (RevitWireType item in this.revitWireTypes)
				{
					this.cmbWireType.Items.Add(item);
				}
				this.cmbWireType.SelectedIndex = 0;
				this.cmbLayMode.Items.Add("AB");
				this.cmbLayMode.Items.Add("BC");
				this.cmbLayMode.Items.Add("AC");
				this.cmbLayMode.Items.Add("CLC");
				this.cmbLayMode.Items.Add("WS");
				this.cmbLayMode.Items.Add("WC");
				this.cmbLayMode.Items.Add("CE");
				this.cmbLayMode.Items.Add("CC");
				this.cmbLayMode.Items.Add("SCE");
				this.cmbLayMode.Items.Add("FC");
				this.cmbLayMode.SelectedIndex = 0;
				this.txtLevel.Text = "3";
			}
			catch (Exception)
			{
				this.Failed = true;
				this.FailedRes = "没有正确获得数据。";
			}
		}

		public void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "Params"));
			string attribute = this.xmlHelper.GetAttribute("WireType");
			if (!string.IsNullOrEmpty(attribute))
			{
				foreach (RevitWireType revitWireType in this.revitWireTypes)
				{
					if (revitWireType.Name == attribute)
					{
						this.cmbWireType.SelectedItem = revitWireType;
						break;
					}
				}
			}
			if (!string.IsNullOrEmpty(this.xmlHelper.GetAttribute("WireNum")))
			{
				this.nudWireNum.Value = Convert.ToInt32(this.xmlHelper.GetAttribute("WireNum"));
			}
			if (this.cmbWireType.Items.Count > 0)
			{
				string attribute2 = this.xmlHelper.GetAttribute("CrossSection");
				if (!string.IsNullOrEmpty(attribute2))
				{
					foreach (double num in (this.cmbWireType.SelectedItem as RevitWireType).WireSizeSet.Keys)
					{
						if (num.ToString() == attribute2)
						{
							this.cmbCrossSection.Text = attribute2;
							break;
						}
					}
				}
			}
			string attribute3 = this.xmlHelper.GetAttribute("LayMode");
			if (!string.IsNullOrEmpty(attribute3))
			{
				this.cmbLayMode.SelectedItem = attribute3;
			}
			string attribute4 = this.xmlHelper.GetAttribute("Level");
			if (!string.IsNullOrEmpty(attribute4))
			{
				this.txtLevel.Text = attribute4;
			}
			string attribute5 = this.xmlHelper.GetAttribute("ConduitTypeID");
			if (!string.IsNullOrEmpty(attribute5))
			{
				foreach (object obj in this.cmbConduitType.Items)
				{
					if ((obj as RevitConduitType).ConduitTypeId.ToString() == attribute5)
					{
						this.cmbConduitType.SelectedItem = obj;
						break;
					}
				}
			}
			if (this.cmbConduitType.Items.Count > 0)
			{
				string attribute6 = this.xmlHelper.GetAttribute("ConduitDiameter");
				if (!string.IsNullOrEmpty(attribute6))
				{
					foreach (int num2 in (this.cmbConduitType.SelectedItem as RevitConduitType).ConduitSizeSet)
					{
						if (num2.ToString() == attribute6)
						{
							this.cmbConduitDiameter.Text = attribute6;
							break;
						}
					}
				}
			}
		}

		public void SaveDlgData()
		{
			try
			{
				this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire"));
				this.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "Params"));
				this.xmlHelper.SetAttribute("WireType", this.cmbWireType.SelectedItem.ToString());
				this.xmlHelper.SetAttribute("WireNum", this.nudWireNum.Value.ToString());
				this.xmlHelper.SetAttribute("CrossSection", this.cmbCrossSection.Text);
				this.xmlHelper.SetAttribute("LayMode", this.cmbLayMode.SelectedItem.ToString());
				this.xmlHelper.SetAttribute("Level", this.txtLevel.Text.Trim());
				this.xmlHelper.SetAttribute("ConduitTypeID", ((RevitConduitType)this.cmbConduitType.SelectedItem).ConduitTypeId.ToString());
				this.xmlHelper.SetAttribute("ConduitDiameter", this.cmbConduitDiameter.Text);
				this.xmlHelper.SaveXml("");
			}
			catch
			{
			}
		}

		private void cmbWireStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
            WireConnectForm.wireParams.WireStyle = (WiringType)this.cmbWireStyle.SelectedIndex;
		}

		private void cmbWireType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cmbCrossSection.Items.Clear();
			WireConnectForm.wireParams.WireType = this.cmbWireType.SelectedItem.ToString();
			RevitWireType revitWireType = this.cmbWireType.SelectedItem as RevitWireType;
			foreach (double num in revitWireType.WireSizeSet.Keys)
			{
				this.cmbCrossSection.Items.Add(num);
			}
			if (revitWireType.WireSizeSet.Count > 0)
			{
				this.cmbCrossSection.SelectedIndex = 0;
			}
			else
			{
				this.cmbCrossSection.Text = "4";
			}
			List<RevitConduitType> conduitTypesForWire = ElectricSettingDataAssist.GetConduitTypesForWire(this.doc, WireConnectForm.wireParams.WireType);
			if (conduitTypesForWire.Count > 0)
			{
				this.cmbConduitType.Items.Clear();
				foreach (RevitConduitType item in conduitTypesForWire)
				{
					this.cmbConduitType.Items.Add(item);
				}
				this.cmbConduitType.SelectedIndex = 0;
			}
			else
			{
				this.cmbConduitType.Items.Clear();
				foreach (RevitConduitType item2 in this.revitConduitTypes)
				{
					this.cmbConduitType.Items.Add(item2);
				}
				this.cmbConduitType.SelectedIndex = 0;
			}
			this.UpdateConduitDiameter();
		}

		private void nudWireNum_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.Control control = sender as System.Windows.Forms.Control;
			if (string.IsNullOrEmpty(control.Text))
			{
				TaskDialog.Show("盈建科", "导线根数不能为空。");
				control.Text = "1";
			}
			int wireNum;
			if (int.TryParse(this.nudWireNum.Value.ToString().Trim(), out wireNum) && this.nudWireNum.Value >= 1m)
			{
				WireConnectForm.wireParams.WireNum = wireNum;
				this.UpdateConduitDiameter();
			}
		}

		private void cmbCrossSection_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.CrossSection = Convert.ToDouble(this.cmbCrossSection.SelectedItem.ToString());
			this.UpdateConduitDiameter();
		}

		private void cmbCrossSection_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cmbCrossSection.Text))
			{
				TaskDialog.Show("盈建科", "截面不能为空。");
				this.cmbCrossSection.Text = "4";
				this.cmbCrossSection.Focus();
				return;
			}
			WireConnectForm.wireParams.CrossSection = Convert.ToDouble(this.cmbCrossSection.Text);
			this.UpdateConduitDiameter();
		}

		private void cmbConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.ConduitType = this.cmbConduitType.SelectedItem.ToString();
			RevitConduitType revitConduitType = this.cmbConduitType.SelectedItem as RevitConduitType;
			WireConnectForm.wireParams.ConduitTypeID = revitConduitType.ConduitTypeId;
			List<int> conduitSizeSet = revitConduitType.ConduitSizeSet;
			this.cmbConduitDiameter.Items.Clear();
			if (conduitSizeSet.Count > 0)
			{
				foreach (int num in conduitSizeSet)
				{
					this.cmbConduitDiameter.Items.Add(num);
				}
				this.cmbConduitDiameter.SelectedIndex = 0;
			}
			else
			{
				this.cmbConduitDiameter.Text = "16";
			}
			this.UpdateConduitDiameter();
		}

		private void cmbConduitDiameter_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.ConduitDiameter = Convert.ToDouble(this.cmbConduitDiameter.SelectedItem.ToString());
		}

		private void cmbConduitDiameter_TextChanged(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cmbConduitDiameter.Text))
			{
				TaskDialog.Show("盈建科", "管径不能为空。");
				this.cmbConduitDiameter.Text = "16";
				this.cmbConduitDiameter.Focus();
				return;
			}
			WireConnectForm.wireParams.ConduitDiameter = Convert.ToDouble(this.cmbConduitDiameter.Text);
		}

		private void UpdateConduitDiameter()
		{
			if (this.cmbWireType.Items.Count > 0 && this.cmbConduitType.Items.Count > 0 && !string.IsNullOrEmpty(this.cmbCrossSection.Text) && this.cmbConduitDiameter.Items.Count > 0)
			{
				int conduitDiameter = ElectricSettingDataAssist.GetConduitDiameter(this.doc, this.cmbWireType.SelectedItem.ToString(), Convert.ToDouble(this.cmbCrossSection.Text), this.cmbConduitType.SelectedItem.ToString());
				double num = (double)this.FindProximal(this.cmbConduitType.SelectedItem.ToString(), conduitDiameter);
				this.cmbConduitDiameter.Text = num.ToString();
			}
		}

		private int FindProximal(string conduitType, int target)
		{
			List<int> list = new List<int>();
			foreach (RevitConduitType revitConduitType in this.revitConduitTypes)
			{
				if (revitConduitType.Name == conduitType)
				{
					list = revitConduitType.ConduitSizeSet;
					break;
				}
			}
			List<int> list2 = new List<int>();
			if (list.Count > 0)
			{
				int num = Math.Abs(target - list[0]);
				foreach (int num2 in list)
				{
					int num3 = Math.Abs(target - num2);
					if (num3 < num)
					{
						num = num3;
						list2.Clear();
						list2.Add(num2);
					}
					else if (num3 == num)
					{
						list2.Add(num2);
					}
				}
			}
			if (list2.Count > 0)
			{
				return list2[0];
			}
			if (list.Count <= 0)
			{
				return 0;
			}
			return list[0];
		}

		private void cmbLayMode_SelectedIndexChanged(object sender, EventArgs e)
		{
			WireConnectForm.wireParams.LayMode = this.cmbLayMode.SelectedItem.ToString();
		}

		private void txtLevel_TextChanged(object sender, EventArgs e)
		{
			if (this.txtLevel.Text.Length > 0)
			{
				double level;
				if (this.txtLevel.Text == "-")
				{
					level = 0.0;
				}
				else
				{
					level = Convert.ToDouble(this.txtLevel.Text.Trim());
				}
				WireConnectForm.wireParams.Level = level;
				return;
			}
			TaskDialog.Show("盈建科", "相对标高参数不能为空。");
			this.txtLevel.Text = "3.5";
			this.txtLevel.SelectAll();
		}

		private void txtLevel_KeyPress(object sender, KeyPressEventArgs e)
		{
			Common.IsDouble(e, this.txtLevel);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void WireParamNon_modeForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
			this.isClose = true;
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgWireParam.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgWireParam.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void WireParamNon_modeForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgWireParam.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		public void IsPlusDouble(KeyPressEventArgs e, string txt)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private void cmbCrossSection_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.cmbCrossSection.Text);
		}

		private void cmbConduitDiameter_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.cmbConduitDiameter.Text);
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "WireParam.xml");

		private const string Root = "Root";

		private const string Node = "HYConnectWire";

		private const string ChildNode = "Params";

		private const string WireStyle = "WireStyle";

		private const string WireType = "WireType";

		private const string WireNum = "WireNum";

		private const string CrossSection = "CrossSection";

		private const string LayMode = "LayMode";

		private const string Level = "Level";

		private const string ConduitDiameter = "ConduitDiameter";

		private const string ConduitTypeID = "ConduitTypeID";

		private Wire baseWire;

		private Document doc;

		public bool isClose;

		private List<RevitWireType> revitWireTypes = new List<RevitWireType>();

		private List<RevitConduitType> revitConduitTypes = new List<RevitConduitType>();

		public bool Failed;

		public string FailedRes = "";
	}
}
